﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAO;
using DTO;
using System.Data;
namespace BUS
{
    public class ChiTietBCTon_BUS
    {

        private static ChiTietBCTon_BUS m_instance = null;

        public static ChiTietBCTon_BUS Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new ChiTietBCTon_BUS();
                return m_instance;
            }
        }


        /// <summary>
        ///   them chi tiet bao cao ton
        /// </summary>
        /// 

        public bool ThemChiTietBaoCaoTon(ChiTietBCTon_DTO _item)
        {
            return ChiTietBCTon_DAO.Instance.ThemChiTietBaoCao(_item.MaBaoCao, _item.MaSanPham, _item.TonDau, _item.PhatSinh, _item.TonCuoi, _item.SoLuongBan, _item.SoLuongMua);
        }

        /// <summary>
        ///   lay danh sach chi tiet baocao ton
        /// </summary>
        /// 

        public List<ChiTietBCTon_DTO> LayDanhSachSanPham(string _id)
        {
            List<ChiTietBCTon_DTO> result = new List<ChiTietBCTon_DTO>();
            DataTable dt = ChiTietBCTon_DAO.Instance.LayToanBoSanPham();
            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
            {
                return null;
            }
            for (int i = 0; i < list.Count;i++ )
            {
                DataRow dr = list[i];
                string id = _id;
                string _masanPham = (string)dr["MaSanPham"];
                int _tondau = LayTonDau(_masanPham);
                int _soluongban = LaySoLuongBan(_masanPham);
                int _soluongmua = LaySoLuongMua(_masanPham);
                int _phatsinh = _soluongban > _soluongmua ? (_soluongban - _soluongmua) : (_soluongmua - _soluongban);
                int _toncuoi = (int)dr["SoLuongTon"];
                ChiTietBCTon_DTO item = new ChiTietBCTon_DTO("", _id, _masanPham, _tondau, _phatsinh, _toncuoi, _soluongban, _soluongmua);
                result.Add(item);
            }
            return result;
        }

        public int LaySoLuongBan(string _id)
        {
            DataTable dt = ChiTietBCTon_DAO.Instance.LaySoLuongBanCuaSanPham(_id);
            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
            {
                return 0;
            }
            int _slBan = 0;
            for (int i = 0; i < list.Count; i++)
            {
                DataRow dr = list[i];
                _slBan += (int)dr["SoLuong"];
            }
            return _slBan;
        }

        public int LaySoLuongMua(string _id)
        {
            DataTable dt = ChiTietBCTon_DAO.Instance.LaySoLuongMuaCuaSanPham(_id);
            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
            {
                return 0;
            }
            int _slMua=0;
            for (int i = 0; i < list.Count;i++ )
            {
                DataRow dr = list[i];
                _slMua += (int)dr["SoLuong"];
            }
            return _slMua;

        }

        public int LayTonDau(string _id)
        {
            DataTable dt = ChiTietBCTon_DAO.Instance.LayTonDauCuaSanPham(_id);
            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
            {
                return 0;
            }
            DataRow dr = list[0];
            int _tondau = (int)dr["TonCuoi"];
            return _tondau;
        }

    }
}
